using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;

namespace XBLib.BusinessObject
{
    public sealed class BusinessObjectHelper
    {
        private static readonly BusinessObjectHelper _instance = new BusinessObjectHelper();

        private BusinessObjectHelper()
        { }

        public static BusinessObjectHelper Instance()
        {
            return _instance;
        }

        public List<T> FillToList<T>(DataTable table) where T : BindableBusinessObject, new()
        {
            List<T> results = new List<T>();
            T item = null;
            foreach (DataRow row in table.Rows)
            {
                item = new T();
                item.GetData(row);
                results.Add(item);
            }
            return results;
        }

        public List<T> FillToList<T>(IDataReader reader) where T : BindableBusinessObject, new()
        {
            List<T> results = new List<T>();
            DataTable table = new DataTable();
            table.Load(reader);
            results = FillToList<T>(table);
            return results;
        }
    }
}
